﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using WebUI.SmartDatingRef;
using SvcUtils;

namespace WebUI {
    public partial class NewMessage : BasePage {
        protected global::System.Web.UI.HtmlControls.HtmlInputButton cmdSend;

        protected void cmdSend_ServerClick(object sender, EventArgs e) {
            if (txtSubject.Value.Trim().Length == 0) {
                Utils.showError(errSpan, "Необходимо задать тему письма.");
                return;
            }

            if (txtBody.Value.Trim().Length == 0) {
                Utils.showError(errSpan, "Необходимо написать текст письма.");
                return;
            }

            byte[] subjBytes = Encoding.Unicode.GetBytes(txtSubject.Value);
            byte[] bodyBytes = Encoding.Unicode.GetBytes(txtBody.Value);
            OperationStatus sendStatus = Client.SendPM(SessionKeyHash, new Guid(cboTo.Value), 
                s(CryptoImpl.cryptAes(subjBytes, b(SessionKey))),
                s(CryptoImpl.cryptAes(bodyBytes, b(SessionKey)))
            );

            if (!Status.SUCCESS(sendStatus)) {
                Utils.showError(errSpan, Status.m(sendStatus));
                return;
            }

            Response.Redirect(Request.Params["ReturnUrl"] != null ?
                Request.Params["ReturnUrl"] :
                "Messages.aspx");
        }

        protected void Page_Load(object sender, EventArgs e) {
            // all possible message destinations
            foreach (MessageFolder mf in new MessageFolder[]{MessageFolder.Inbox, MessageFolder.Sent}) {
                ResultWithStatusOfArrayOfMsgswc4fAsa msgsData = 
                    Client.GetPMs(SessionKeyHash, mf);
                if (Status.SUCCESS(msgsData.Status)) {
                    foreach (Msg m in msgsData.Result) {
                        PersonalData senderPd = (mf == MessageFolder.Inbox ? m.MembershipFrom : m.MembershipTo).PersonalData;
                        string senderName = senderPd.first_name + " " + senderPd.last_name;
                        string senderId = (mf == MessageFolder.Inbox ? m.usr_from : m.usr_to).ToString();
                        if (cboTo.Items.FindByValue(senderId) == null) {
                            cboTo.Items.Add(new ListItem(senderName, senderId));
                        }
                    }
                }
            }

            if (Request.Params["re"] != null) {
                // reply
                ResultWithStatusOfMsgswc4fAsa msgRes = Client.GetPM(SessionKeyHash,
                    new Guid(Request.Params["re"]));
                if (Status.SUCCESS(msgRes.Status)) {
                    Msg msg = msgRes.Result;
                    cboTo.Value = msg.usr_from.ToString();
                    cboTo.Disabled = true;
                    txtSubject.Value = "Re: " + msg.msg_subject;
                    txtBody.Value = "\n" +
                                   "--- Original Message ---\n" +
                                   msg.msg_text;
                }
            } else if (Request.Params["to"] != null) {
                // fixed-destination new message
                cboTo.Items.Clear();
                cboTo.Items.Add(new ListItem(Request.Params["nm"], Request.Params["to"]));
                cboTo.Disabled = true;
            } else {
                // new message. no restrictions and preparations.
            }
        }
    }
}